package com.sticktogether.pushnotifications;

import static com.sticktogether.pushnotifications.CommonUtilities.SENDER_ID;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;

import com.google.android.gcm.GCMRegistrar;

public class Register {
	
	static AsyncTask<Void, Void, Void> mRegisterTask;
	private static Context context;

	/**
	 * Check if we are already registered for GCM, if not, register!
	 * 
	 * @param context2
	 */
	public static void register(final Context context2) {
		
		context = context2;
		
		ConnectionDetector cd = new ConnectionDetector(context.getApplicationContext());
		// Check if Internet present
		if (!cd.isConnectingToInternet()) {
			// Internet Connection is not present
			// stop executing code by return
			return;
		}
		
		/*// Make sure the device has the proper dependencies.
		GCMRegistrar.checkDevice(context);
		// Make sure the manifest was properly set - comment out this line
		// while developing the app, then uncomment it when it's ready.
		GCMRegistrar.checkManifest(context);*/
		
		// Get GCM registration id
		final String regId = GCMRegistrar.getRegistrationId(context);

		if (regId.equals("")) {
			// Registration is not present, register now with GCM			
			GCMRegistrar.register(context, SENDER_ID);
		}
		else {
			if (GCMRegistrar.isRegisteredOnServer(context)) {
				// Device is already registered on our server.
			} else {
				registeronserver();
			}
		}
	}
	
	/**
	 * OK, we are not registered on the server, so do it now!
	 */
	static void registeronserver() {
		// register, but not in the UI thread.
		// It's also necessary to cancel the thread onDestroy(),
		// hence the use of AsyncTask instead of a raw thread.
		
        SharedPreferences settings;
        settings = context.getSharedPreferences("Config1", 0);
        final String userid = settings.getString("id", "") ;
        final String pw = settings.getString("sid", "") ;
        
		// Get GCM registration id
		final String regId = GCMRegistrar.getRegistrationId(context);
        
		mRegisterTask = new AsyncTask<Void, Void, Void>() {

			@Override
			protected Void doInBackground(Void... params) {
				// Register on our server
				// The regId is added to the user in our database
				ServerUtilities.register((Context) context, regId, userid, pw);
				return null;
			}

			@Override
			protected void onPostExecute(Void result) {
				mRegisterTask = null;
			}

		};
		mRegisterTask.execute(null, null, null);
	}
	
	/**
	 * We want to unregister from GCM (because we want to logout).
	 * 
	 * @param context2
	 */
	public static void unregister(final Context context2) {
		context = context2;
		GCMRegistrar.unregister(context);

		final String regId = GCMRegistrar.getRegistrationId(context);
		
        SharedPreferences settings;
        settings = context.getSharedPreferences("Config1", 0);
        final String userid = settings.getString("id", "") ;
        final String pw = settings.getString("sid", "") ;

		mRegisterTask = new AsyncTask<Void, Void, Void>() {

			@Override
			protected Void doInBackground(Void... params) {
				// unregister on our server
				// the regId is removed from our database
				ServerUtilities.unregister(context, regId, userid, pw);
				return null;
			}

			@Override
			protected void onPostExecute(Void result) {
				mRegisterTask = null;
			}

		};
		mRegisterTask.execute(null, null, null);
	}
	

}
